package keter.domain.base;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;

@MappedSuperclass
public abstract class BaseEntity {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(BaseEntity.class);
	
	protected Long id;
	
	private String remark;
	
//  声明一个策略通用生成器，name为"system-uuid",策略strategy为"uuid"。
//	@GenericGenerator(name = "system-uuid", strategy = "uuid")
//  用generator属性指定要使用的策略生成器。
//	@GeneratedValue(generator = "system-uuid")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(length=100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	
    
    // 如果不指定类型，mysql会用“datetime”作为字段类型
    /*
     * 需要数据库支持 - MySQL生效方式： alter TABLE `user` MODIFY `CREATED` TIMESTAMP NOT
     * NULL DEFAULT CURRENT_TIMESTAMP alter table `user` type=InnoDB
     */
    // @org.hibernate.annotations.Generated(org.hibernate.annotations.GenerationTime.INSERT)
    // @Column(insertable = false, updatable = false)
    private Date created = new Date();
    
    // 设定JSON序列化时的日期格式
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getCreated() {
        return created;
    }
    
    public void setCreated(Date created) {
        this.created = created;
    }
    
    public String decode(String s) {
    	s = StringUtils.isEmpty(s)?"":s;
    	try {
			return URLDecoder.decode(s,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("URL解码异常",e);
		}
		return s;
    }
}
